package com.vinaprime.primeschedule.businesslayer;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.vinaprime.primeschedule.databaselayer.VPGroupHeader_DL;
import com.vinaprime.primeschedule.databaselayer.VPProject_DL;
import com.vinaprime.primeschedule.databaselayer.VPTaskList_DL;
import com.vinaprime.primeschedule.entities.VPGroupHeader;
import com.vinaprime.primeschedule.entities.VPProject;
import com.vinaprime.primeschedule.entities.VPTaskList;

public class VPTaskList_BL {

	private VPTaskList_DL myTaskListDL ;
	private DateFormat myDbDateFormat, myDateFormat;
	public String getDbDateformat()
	{
		return VPTaskList_DL.getDbDateformat();
	}
	public VPTaskList_BL (Context context , String path){
		this.myTaskListDL = new VPTaskList_DL(context, path);
		
		this.myDbDateFormat = new SimpleDateFormat(VPTaskList_DL.getDbDateformat());
		this.myDateFormat = DateFormat.getDateInstance();
	}

	public int getNewTaskId (){
		
		int result = 0;
		Cursor cur =  myTaskListDL.getNewTaskID();
		if (cur.moveToFirst())
		{
			if (!cur.isNull(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskId()))) {
				result = Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskId())));
			}
		}
		return result+1;
	}

	public Cursor getAllTaskListToCursor() throws IllegalArgumentException, ParseException{
		Cursor cur = this.myTaskListDL.getAllTaskList();
		if(cur != null)
		{
			cur.moveToFirst();
		}
		
		return cur;
	}
	public VPTaskList getTaskbyID (int Id) throws IllegalArgumentException, ParseException{
		VPTaskList myTask = new VPTaskList();
		Date fromDate, toDate;
		Cursor cur  =  this.myTaskListDL.getTaskbyId(Id);
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{			
				myTask.setVP_TASK_ID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskId()))));
				myTask.setVP_TASK_NAME(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskName())));
				myTask.setVP_TASK_CONTENT(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskContent())));
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskStartDate())))
				{
					fromDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskStartDate())));
					myTask.setVP_TASK_START_DATE(this.myDateFormat.parse(this.myDateFormat.format(fromDate)));
				}
				
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskFinishDate())))
				{
					toDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskFinishDate())));
					myTask.setVP_TASK_FINISH_DATE(this.myDateFormat.parse(this.myDateFormat.format(toDate)));
				}
				myTask.setVP_TASK_ESTIMATE_TIME(Float.parseFloat(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskEstimateTime()))));				
				myTask.setVP_TASK_ACTUAL_TIME(Float.parseFloat(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskActualTime()))));				
				myTask.setVP_TASK_REMIND(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskRemind()))));
				myTask.setVP_TASK_NOTE(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskNote())));
				myTask.setVP_TASK_ASSIGN(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskAssign())));
				myTask.setVP_TASK_GROUP(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskGroup()))));
				myTask.setVP_TASK_IS_ASSIGNED(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskIsAssigned())));
				myTask.setVP_TASK_COMPLETE(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskComplete()))));
				myTask.setVP_TASK_PROJECT(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskProject()))));
				myTask.setVP_TASK_FILE(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskFile())));
				
				cur.moveToNext();
			}
		}
		return myTask;		
	}
	public int TotalSize()
	{
		Cursor cur = this.myTaskListDL.getAllTaskList();
		return cur.getCount();
	}
	public List<VPTaskList> getAllTaskList() throws IllegalArgumentException, ParseException{
		List<VPTaskList> myTaskList = new ArrayList<VPTaskList>();
		
		Date fromDate, toDate;
		Cursor cur = this.myTaskListDL.getAllTaskList();
		
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{	
	           	VPTaskList myTask = new VPTaskList(); 
            	myTask.setVP_TASK_ID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskId()))));
				myTask.setVP_TASK_NAME(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskName())));
				myTask.setVP_TASK_CONTENT(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskContent())));
				/*
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskStartDate())))
				{
					fromDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskStartDate())));
					myTask.setVP_TASK_START_DATE(this.myDateFormat.parse(this.myDateFormat.format(fromDate)));
				}
				
				if(!cur.isNull(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskFinishDate())))
				{
					toDate = this.myDbDateFormat.parse(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskFinishDate())));
					myTask.setVP_TASK_FINISH_DATE(this.myDateFormat.parse(this.myDateFormat.format(toDate)));
				}*/
				myTask.setVP_TASK_ESTIMATE_TIME(Float.parseFloat(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskEstimateTime()))));				
				myTask.setVP_TASK_ACTUAL_TIME(Float.parseFloat(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskActualTime()))));				
				
				myTask.setVP_TASK_REMIND(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskRemind()))));
				myTask.setVP_TASK_NOTE(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskNote())));
				myTask.setVP_TASK_ASSIGN(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskAssign())));
				//myTask.setVP_TASK_GROUP(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskGroup()))));
				myTask.setVP_TASK_IS_ASSIGNED(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskIsAssigned())));
				myTask.setVP_TASK_COMPLETE(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskComplete()))));
				myTask.setVP_TASK_PROJECT(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskProject()))));
				myTask.setVP_TASK_FILE(cur.getString(cur.getColumnIndexOrThrow(VPTaskList_DL.getVpTaskFile())));
				
				myTaskList.add(myTask);
                cur.moveToNext();
			}
		}
		
	return myTaskList;
		
	}
	
}
